package com.hanson.service.impl;

import com.hanson.mapper.*;
import com.hanson.pojo.vo.*;
import com.hanson.service.InitService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @description:
 * @classname: InitServiceImpl
 * @author: Hanson
 * @create: 2021/01/14
 **/
@Slf4j
@Service
public class InitServiceImpl implements InitService {

    @Autowired
    LogisticsMapper logisticsMapper;

    @Autowired
    LogisticsRecordMapper logisticsRecordMapper;

    @Autowired
    MemberOrderMapper memberOrderMapper;

    @Autowired
    GoodsMapper goodsMapper;

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    MemberMapper memberMapper;

    @Override
    public void init() {
        //获取20用户
        List<Member> members = memberMapper.doGetMembers();
        Set<Integer> mSet = new HashSet<>();
        //以members长度作为参考随机生成20个索引
        for (int i = 0; i < 10000; i++) {
            if (mSet.size() >= 20){
                break;
            }
            int nextInt = new Random().nextInt(members.size());
            mSet.add(nextInt);
        }
        //获取5个商品
        List<Goods> goods = goodsMapper.doGetGoods();
        Set<Integer> gSet = new HashSet<>();
        //以goods长度作为参考随机生成5个索引
        for (int i = 0; i < 10000; i++) {
            if (gSet.size() >= 5){
                break;
            }
            int nextInt = new Random().nextInt(goods.size());
            gSet.add(nextInt);
        }
        //获取物流公司
        List<Logistics> logistics = logisticsMapper.doGetAllLogistics(1);
        Set<Integer> lSet = new HashSet<>();
        //以logistics长度作为参考随机生成5个索引
        for (int i = 0; i < 10000; i++) {
            if (lSet.size() >= 5){
                break;
            }
            int nextInt = new Random().nextInt(5);
            lSet.add(nextInt);
        }
        //遍历用户，为用户生成购买订单
        for (Integer mIdx : mSet) {
            //获取用户
            Member member = members.get(mIdx);
            //为用户生成五个订单，每个订单5个商品
            for (int i = 0; i < 5; i++) {
                MemberOrder memberOrder = new MemberOrder();
                //模拟订单id
                String temp = String.valueOf(Math.random());//[0,1)
                String ran = temp.substring(temp.indexOf(".") + 1, temp.indexOf(".") + 4);
                memberOrder.setOrderId(System.currentTimeMillis() + ran);
                int quantity = new Random().nextInt(3) + 1;
                memberOrder.setQuantity(quantity);
                memberOrder.setUserId(String.valueOf(member.getMemberId()));
                //每个订单购买五个商品
                for (Integer gIdx : gSet) {
                    memberOrder.setProductId(goods.get(gIdx).getProductId());
                    //创建用户订单
                    System.out.println(memberOrder.toString());
                    memberOrderMapper.doCreateMemberOrder(memberOrder);
                }
            }
        }
        //获取全部用户订单
        List<MemberOrder> memberOrders = memberOrderMapper.doGetMemberOrders();
        //分组过滤
        Set<String> orderIds = memberOrders.stream().map(MemberOrder::getOrderId).collect(Collectors.toSet());
        //生成订单
        for (String orderId : orderIds) {
            //每个订单id对应的用户订单列表数据
            List<MemberOrder> memberOrders1 = memberOrderMapper.doGetMemberOrderByoid(orderId);
            //用户id
            String userId = null;
            BigDecimal sum = BigDecimal.ZERO;
            //遍历用户订单获取购买的全部商品并统计
            for (MemberOrder memberOrder : memberOrders1) {
                Integer productId = memberOrder.getProductId();
                Integer quantity = memberOrder.getQuantity();
                userId = memberOrder.getUserId();
                //获取商品信息
                Goods gd = goodsMapper.doGetGoodsById(productId);
                BigDecimal price = gd.getPrice();
                sum = sum.add(new BigDecimal(quantity).multiply(price));
            }
            //设置订单
            Date createTime = new Date();
            Date payTime;
            Date closeTime;
            Date finishTime;
            Long recordId = null;
            int state = new Random().nextInt(8) + 1;
//            SUCCESS="交易完成";      //订单状态为1
//            RECEIVED="已签收";      //订单状态为2
//            TO_BE_RECEIVED="待签收";//订单状态为3
//            DELIVERED="已发货";     //订单状态为4
//            PAID="已支付";          //订单状态为5
//            TO_BE_PAID="待支付";    //订单状态为6
//            CLOSE="交易取消";        //订单状态为7
//            RETURN="发起退单";       //退单，订单状态为8
//            RUBBISH="已回收";    //订单状态为1000
            if (state == 1 | state == 8){
                payTime= new Date();
                closeTime = null;
                finishTime = new Date();
            }else if (state == 2 | state == 3 | state == 4 | state ==5){
                payTime =new Date();
                closeTime =null;
                finishTime =null;
            }else if (state == 6){
                payTime = null;
                closeTime =null;
                finishTime =null;
            }else if (state == 7){
                payTime = null;
                closeTime = new Date();
                finishTime =null;
            }else {
                payTime = null;
                closeTime = null;
                finishTime = null;
            }
            if (!(state == 5 | state == 6 | state == 7)){
                //模拟物流
                String temp = String.valueOf(Math.random());//[0,1)
                String ran = temp.substring(temp.indexOf(".") + 1, temp.indexOf(".") + 4);
                recordId = Long.valueOf(String.valueOf(System.currentTimeMillis() + ran));
            }
            Order order = new Order(
                    orderId,
                    userId,
                    recordId,
                    sum,
                    null,
                    createTime,
                    payTime,
                    closeTime,
                    finishTime,
                    state
            );
            //生成订单
            System.out.println(order.toString());
            orderMapper.doCreateOrder(order);
            //为订单物流号选择物流公司并生成物流记录
            if (recordId != null && recordId > 0){
                int nextInt = new Random().nextInt(5);
                List<Integer> listIdx = new ArrayList<>(lSet);//索引的索引数字
                Integer logisticId = logistics.get(listIdx.get(nextInt)).getId();//索引指向的数据
                //创建物流信息
                LogisticsRecord record = new LogisticsRecord();
                record.setLogisticsId(logisticId);
                record.setRecordId(recordId);
                System.out.println(record.toString());
                logisticsRecordMapper.doCreateLogisticsRecord(record);
            }
        }
    }


}
